package com.clj.blesample.menu;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.clj.blesample.BaseActivity;
import com.clj.blesample.R;
import com.clj.blesample.compose.BaseTransformer;
import com.clj.blesample.databinding.ActivityRecordListBinding;
import com.clj.blesample.entity.RecordInfo;
import com.clj.blesample.http.retrofit.RetrofitManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import io.reactivex.observers.DisposableObserver;

public class RecordListActivity extends BaseActivity {
    private ActivityRecordListBinding mBinding;
    private BaseQuickAdapter<RecordInfo, BaseViewHolder> mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityRecordListBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        setSupportActionBar(mBinding.toolbar);
        getSupportActionBar().setTitle("电熔记录查询");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        mBinding.inputMachineNo.getEtContent().setInputType(InputType.TYPE_CLASS_NUMBER);
        mBinding.inputCollectNo.getEtContent().setInputType(InputType.TYPE_CLASS_NUMBER);
//        if (BuildConfig.DEBUG) {
//            mBinding.inputProjectNo.setContent("RO-TEST-230518");
//            mBinding.inputCollectNo.setContent("1");
//        }
        DividerItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(Objects.requireNonNull(ContextCompat.getDrawable(this, R.drawable.inset_recyclerview_divider)));
        mBinding.rvList.addItemDecoration(itemDecoration);
        mBinding.rvList.setAdapter(mAdapter = new BaseQuickAdapter<RecordInfo, BaseViewHolder>(R.layout.item_record) {
            @Override
            protected void convert(@NonNull BaseViewHolder holder, RecordInfo recordInfo) {
                holder.setText(R.id.tvTitle, recordInfo.F_ProjectCode)
                        .setText(R.id.tvContent, recordInfo.F_WeldNum);
                TextView tvStatus = holder.getView(R.id.tvStatus);
                tvStatus.setTextColor("合格".equals(recordInfo.F_AnalysisResult) ? Color.GREEN : Color.RED);
                tvStatus.setText(TextUtils.isEmpty(recordInfo.F_AnalysisResult) ? "异常" : recordInfo.F_AnalysisResult);
            }
        });
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                startActivity(new Intent(RecordListActivity.this, RecordDetailActivity.class).putExtra("data", mAdapter.getItem(position)));
            }
        });
        mAdapter.setEmptyView(R.layout.empty_view);
        mBinding.tvQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showProgress();
                String projectNo = mBinding.inputProjectNo.getContent();
                String machineNo = mBinding.inputMachineNo.getContent();
                String collectNo = mBinding.inputCollectNo.getContent();
                Map<String, Object> params = new HashMap<>();
                params.put("F_ProjectCode", projectNo);
                params.put("F_MachineCode", machineNo);
                params.put("F_WeldNum", collectNo);
                RetrofitManager.getApi().getAnalysisResult(params)
                        .compose(new BaseTransformer<>())
                        .subscribe(new DisposableObserver<List<RecordInfo>>() {
                            @Override
                            public void onNext(List<RecordInfo> data) {
                                mAdapter.setNewInstance(data);
                            }

                            @Override
                            public void onError(Throwable e) {
                                e.printStackTrace();
                                mAdapter.setNewInstance(null);
                                hideProgress();
                            }

                            @Override
                            public void onComplete() {
                                hideProgress();
                            }
                        });
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
